package com.ruoyi.whzj.controller;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.whzj.domain.WhBalancerecord;
import com.ruoyi.whzj.domain.WhUser;
import com.ruoyi.whzj.service.IWhBalancerecordService;
import com.ruoyi.whzj.service.IWhUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whzj.domain.WhUserwithdraw;
import com.ruoyi.whzj.service.IWhUserwithdrawService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户提现记录Controller
 * 
 * @author ruoyi
 * @date 2020-10-30
 */
@Controller
@RequestMapping("/whzj/userwithdraw")
public class WhUserwithdrawController extends BaseController
{
    private String prefix = "whzj/userwithdraw";

    @Autowired
    private IWhUserwithdrawService whUserwithdrawService;
    @Autowired
    private IWhUserService whUserService;
    @Autowired
    private IWhBalancerecordService whBalancerecordService;
    @RequiresPermissions("whzj:userwithdraw:view")
    @GetMapping()
    public String userwithdraw()
    {
        return prefix + "/userwithdraw";
    }

    /**
     * 查询用户提现记录列表
     */
    @RequiresPermissions("whzj:userwithdraw:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WhUserwithdraw whUserwithdraw)
    {
        startPage();
        List<WhUserwithdraw> list = whUserwithdrawService.selectWhUserwithdrawList(whUserwithdraw);
        return getDataTable(list);
    }


    /**
     * 查询用户申请提现记录列表
     */
    @RequiresPermissions("whzj:userwithdraw:list")
    @PostMapping("/listBegin")
    @ResponseBody
    public TableDataInfo listBegin(WhUserwithdraw whUserwithdraw)
    {
        startPage();
        whUserwithdraw.setStatus("申请");
        List<WhUserwithdraw> list = whUserwithdrawService.selectWhUserwithdrawList(whUserwithdraw);
        return getDataTable(list);
    }

    /**
     * 查询用户已提现记录列表
     */
    @RequiresPermissions("whzj:userwithdraw:list")
    @PostMapping("/listEnd")
    @ResponseBody
    public TableDataInfo listEnd(WhUserwithdraw whUserwithdraw)
    {
        startPage();
        whUserwithdraw.setStatus("已转账");
        List<WhUserwithdraw> list = whUserwithdrawService.selectWhUserwithdrawList(whUserwithdraw);
        return getDataTable(list);
    }

    /**
     * 导出用户提现记录列表
     */
    @RequiresPermissions("whzj:userwithdraw:export")
    @Log(title = "用户提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WhUserwithdraw whUserwithdraw)
    {
        List<WhUserwithdraw> list = whUserwithdrawService.selectWhUserwithdrawList(whUserwithdraw);
        ExcelUtil<WhUserwithdraw> util = new ExcelUtil<WhUserwithdraw>(WhUserwithdraw.class);
        return util.exportExcel(list, "userwithdraw");
    }

    /**
     * 新增用户提现记录
     */
    @GetMapping("/userwithdrawbegin")
    public String userwithdrawbegin()
    {
        return prefix + "/userwithdrawbegin";
    }

    /**
     * 新增用户提现记录
     */
    @GetMapping("/userwithdrawend")
    public String userwithdrawend()
    {
        return prefix + "/userwithdrawend";
    }

    /**
     * 新增用户提现记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户提现记录
     */
    @RequiresPermissions("whzj:userwithdraw:add")
    @Log(title = "用户提现记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WhUserwithdraw whUserwithdraw)
    {
        return toAjax(whUserwithdrawService.insertWhUserwithdraw(whUserwithdraw));
    }

    /**
     * 修改用户提现记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        WhUserwithdraw whUserwithdraw = whUserwithdrawService.selectWhUserwithdrawById(id);
        mmap.put("whUserwithdraw", whUserwithdraw);
        return prefix + "/edit";
    }

    /**
     * 审批拒绝
     */
    @GetMapping("/review/{id}")
    public String review(@PathVariable("id") String id, ModelMap mmap)
    {
        WhUserwithdraw whUserwithdraw = whUserwithdrawService.selectWhUserwithdrawById(id);
        mmap.put("id", id);
        mmap.put("whUserwithdraw",whUserwithdraw);
        return prefix + "/review";
    }


    /**
     * 拒绝转账
     */
    @RequiresPermissions("whzj:userwithdraw:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewDis")
    @ResponseBody
    public AjaxResult reviewDis(WhUserwithdraw whUserwithdraw)
    {
        DecimalFormat df=new DecimalFormat("0.0");
        WhUserwithdraw temp = new WhUserwithdraw();
        temp.setId(whUserwithdraw.getId());
        temp.setReduceReason(whUserwithdraw.getReduceReason());
        temp.setStatus("失败");
        int num = whUserwithdrawService.updateWhUserwithdraw(temp);
        WhUserwithdraw whUserwithdraw1 =  whUserwithdrawService.selectWhUserwithdrawById(whUserwithdraw.getId());
        WhUser whUser = whUserService.selectWhUserById(whUserwithdraw1.getUserId());
        if(num >0){
            //把钱退回用户余额
            WhUser  whUser1 = new WhUser();
            whUser1.setId(whUserwithdraw1.getUserId());
            whUser1.setBalance(whUser.getBalance().add(whUserwithdraw1.getWithdrawMoney()));
            whUserService.updateWhUser(whUser1);
            //添加余额变动
            WhBalancerecord whBalancerecord = new WhBalancerecord();
            whBalancerecord.setOperate("7");
            whBalancerecord.setUserId(whUserwithdraw1.getUserId());
            whBalancerecord.setBalance(whUserwithdraw1.getWithdrawMoney());

            whBalancerecord.setFront(whUser.getBalance());
            whBalancerecord.setAfter(whUser1.getBalance());
            whBalancerecordService.insertWhBalancerecord(whBalancerecord);
        }
        return toAjax(num);
    }

    /**
     * 同意转账
     */
    @RequiresPermissions("whzj:userwithdraw:edit")
    @Log(title = "同意转账", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewSucc")
    @ResponseBody
    public AjaxResult reviewSucc(String id)
    {
        WhUserwithdraw temp = new WhUserwithdraw();
        temp.setId(id);
        temp.setStatus("已转账");
        return toAjax(whUserwithdrawService.updateWhUserwithdraw(temp));
    }

    /**
     * 修改保存用户提现记录
     */
    @RequiresPermissions("whzj:userwithdraw:edit")
    @Log(title = "用户提现记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WhUserwithdraw whUserwithdraw)
    {
        return toAjax(whUserwithdrawService.updateWhUserwithdraw(whUserwithdraw));
    }

    /**
     * 删除用户提现记录
     */
    @RequiresPermissions("whzj:userwithdraw:remove")
    @Log(title = "用户提现记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(whUserwithdrawService.deleteWhUserwithdrawByIds(ids));
    }

    /**
     * 添加转账凭证
     */
    @GetMapping("/addPic/{id}")
    public String addPic(@PathVariable("id") String id, ModelMap mmap)
    {
        WhUserwithdraw whUserwithdraw = whUserwithdrawService.selectWhUserwithdrawById(id);
        mmap.put("id",id);
        mmap.put("whUserwithdraw",whUserwithdraw);
        return prefix + "/zhuanzhang";
    }

    /**
     * 查询汇总
     */
    @RequiresPermissions("whzj:userwithdraw:list")
    @PostMapping("/getCount")
    @ResponseBody
    public AjaxResult getCount(String type,String userName, String status) throws ParseException {
        Map<String,Object> resultMap = new HashMap<>();
        WhUserwithdraw whUserwithdraw = new WhUserwithdraw();
        whUserwithdraw.setUserName(userName);
        whUserwithdraw.setStatus(status);
        if("end".equals(type)){
            whUserwithdraw.setStatus("已转账");
        }
        if("begin".equals(type)){
            whUserwithdraw.setStatus("申请");
        }
        List<WhUserwithdraw> list = whUserwithdrawService.selectWhUserwithdrawList(whUserwithdraw);
        BigDecimal sumMoney = new BigDecimal("0.0");
        for(WhUserwithdraw temp:list){
            sumMoney = sumMoney.add(temp.getWithdrawMoney());
        }

        resultMap.put("sumMoney",sumMoney);
        return  new AjaxResult(AjaxResult.Type.SUCCESS,"200",resultMap);
    }

}
